package com.txsj.scan.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.WarehouseBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * Created by wangchuangjun on 2018/11/9
 * Desc: 选择仓库适配器
 */


public class SelectWarehouseAdapter extends RecyclerView.Adapter<SelectWarehouseAdapter.CommpanyViewHolder> {

    private Activity activity;
    private List<WarehouseBean> warehouseList;
    private List<WarehouseBean> showWarehouseList;

    public SelectWarehouseAdapter(Activity activity) {
        this.activity = activity;
        this.warehouseList = new ArrayList<>();
        this.showWarehouseList = new ArrayList<>();
    }

    public void setData(List<WarehouseBean> warehouseList) {
        this.warehouseList = warehouseList;
        notifyDataSetChanged();
    }

    /**
     * 隐藏标签数量为0的仓库item
     */
    public void upDateList(){
        for (WarehouseBean warehouseBean:warehouseList){
            if (null != warehouseBean.getEpcList() && warehouseBean.getEpcList().size() > 0 &&
                    warehouseBean.getCount() > 0){
                if (showWarehouseList.contains(warehouseBean))continue;
                showWarehouseList.add(warehouseBean);
            }
        }
        //按照仓库id进行排序
        Collections.sort(showWarehouseList, new Comparator<WarehouseBean>() {
            @Override
            public int compare(WarehouseBean o1, WarehouseBean o2) {
                return o1.getWarehouse_id() - o2.getWarehouse_id();
            }
        });
        notifyDataSetChanged();
    }

    public List<WarehouseBean>  getShowWarehouseList(){
        return showWarehouseList;
    }

    /**
     * 获取某一个仓库
     * @param position
     * @return
     */
    public WarehouseBean getWarehouse(int position){
        return showWarehouseList.get(position);
    }

    /**
     * 获取仓库列表
     * @return
     */
    public List<WarehouseBean> getWarehouseList(){
        return warehouseList;
    }

    public void clear(){
        if (warehouseList.size() == 0)return;
        for (WarehouseBean warehouseBean:warehouseList){
            warehouseBean.setCount(0);
            if (null != warehouseBean.getEpcList() && warehouseBean.getEpcList().size() > 0) {
                warehouseBean.getEpcList().clear();//如果存在EpcList集合，为了防止在扫描时间很短的情况下显示已结算的数据量不准确
            }
        }
        showWarehouseList.clear();
        notifyDataSetChanged();
    }

    @Override
    public CommpanyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CommpanyViewHolder(LayoutInflater.from(activity).inflate(R.layout.item_ware_house, parent, false));
    }

    @Override
    public void onBindViewHolder(CommpanyViewHolder holder, final int position) {
        WarehouseBean warehouse = showWarehouseList.get(position);
        holder.tv_warehouse.setText(warehouse.getWarehouse_name());
        if (warehouse.getWarehouse_name().contains("总仓")){
            holder.tv_warehouse.setTextColor(activity.getResources().getColor(R.color.red));
        }else {
            holder.tv_warehouse.setTextColor(activity.getResources().getColor(R.color.text_color_normal));
        }
        holder.tv_count.setText("" + warehouse.getCount());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onCLick(position);
            }
        });
        if(null != warehouse && null != warehouse.getEpcList() && warehouse.getEpcList().size() > 0){
            holder.itemView.setVisibility(View.VISIBLE);
        }else {
            holder.itemView.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return showWarehouseList.size();
    }

    public class CommpanyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_count, tv_warehouse;

        public CommpanyViewHolder(View itemView) {
            super(itemView);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);
            tv_warehouse = (TextView) itemView.findViewById(R.id.tv_warehouse);
        }
    }

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public static interface OnItemClickListener {
        void onCLick(int position);
    }
}
